--[[
   @brief    新版社交客户端承载
   @author   senpingtian
   @version  1.0
   @date     2019-11-08
--]]

pwg_social_control = {
    SESSION_STATE_OPEN = 1;
    UNREAD_NOTIFY_NEW = 0;
    UNREAD_NOTIFY_REFRESH = 1;
    UNREAD_NOTIFY_NEW_ROOM_MSG = 2;
    UNREAD_NOTIFY_NEW_GENERIC_MSG = 3;

    SESSION_TYPE_1V1_SESSION = 1;
    SESSION_TYPE_ROOM = 2;
};

function pwg_social_control:log(msg, core_msg)
    base_utility.log("[pwg_social_control]" .. msg, core_msg);
end;

function pwg_social_control:init()
    self:log("init.");
    ierd_tgp.tpf_ui.Require("social\\pwg_social\\model.lua");
    ierd_tgp.tpf_ui.Require("social\\pwg_social\\view.lua");
    pwg_social_view:init();
    pwg_social_model:init();

    comm_center.reg_service("i_service_show_pwg_social_window", 
                            "pwg_social_control:on_sevice_show_window");
    comm_center.reg_service("i_service_new_social_open_panel",
                            "pwg_social_control:on_open_pwg_panel");
    comm_center.listen_broadcast("i_broadcast_new_social_unread_msg_notify",
                                 "pwg_social_control:on_unread_msg_notify");
    comm_center.listen_broadcast("i_broadcast_new_social_session_state_changed",
                                 "pwg_social_control:on_session_state_changed");
    comm_center.listen_broadcast("i_broadcast_new_social_apply_add_friend_notify",
                                 "pwg_social_control:on_apply_add_friend");

    comm_center.listen_broadcast("i_service_new_social_page_nav_complete",
                                 "pwg_social_control:on_social_page_nav_completed");
    comm_center.listen_broadcast("i_service_new_social_set_session_notice_status_rsp",
                                 "pwg_social_control:on_set_session_notice_status_rsp");
    comm_center.listen_broadcast("i_broadcast_new_social_user_muted_list_rsp", 
                                 "pwg_social_control:on_query_user_muted_list_rsp");
end;

function pwg_social_control:on_sevice_show_window(param)
    local show_window = true;
    if nil ~= param and nil ~= param.show then
        show_window = param.show;
    end

    if show_window then
        local dlg_ptr, created_newly = pwg_social_view:create_window(param.url);
        if dlg_ptr then
            if not created_newly then
                if nil ~= param.url and "" ~= param.url then
                    pwg_social_view:load_page(param.url);
                end
                -- active or restore
                pwg_social_view:show_window();
            end
        end
    else
        pwg_social_view:hide_window();
    end
    comm_center.send_broadcast("i_broadcast_new_social_window_show_changed",
                                { show = show_window });
end;

function pwg_social_control:on_show(uilib, ctrl_id)
    self:log("on_show");
end

function pwg_social_control:on_hide(uilib, ctrl_id)
    self:log("on_hide");
    pwg_social_view:close_window();
end

function pwg_social_control:on_close(uilib, ctrl_id)
    self:log("on_close");
    comm_center.call_service("i_service_social_close_window", {});
    pwg_social_view:close_window();
end

function pwg_social_control:on_js_callback(uilib, func_name, json)
    if json == nil or json == "" or func_name == nil or func_name == "" then
        return;
    end
    self:log(string.format("on_js_callback, func_name = %s, json = %s", func_name, json));
    if func_name == "pwg_social_set_game_invitation_result" then
        local param = base_utility.json_to_table(json);
        self:set_game_invitation_result(base_utility.table_to_json(param.data or {}));
    elseif func_name == "pwg_social_query_game_info_by_id" then
        local param = base_utility.json_to_table(json);
        param.custom_id = CID_WEB_CTRL_FOR_NEW_SOCIAL;
        event_center.send_event(extendboard_event.QUERY_GAME_INFO_BY_ID_V2,
                                base_utility.table_to_json(param));
    elseif func_name == "pwg_social_set_notice_status" then
        self:js_set_notice_status(json);
    elseif func_name == "pwg_social_query_muted_user_list" then
        self:js_query_muted_user_list(json);
    elseif func_name == "pwg_social_set_social_remark_notify" then
        local param = base_utility.json_to_table(json);
        event_center.send_event(group_event.UPDATE_FRIEND_REMARKS,
                                base_utility.table_to_json(param.data or {}));
    end
end

function pwg_social_control:on_web_crash()
    self:log("on_web_crash");
    pwg_social_view:on_web_crashed();
end

function pwg_social_control:on_close_btn_click()
    self:log("on_close_btn_click");
    pwg_social_view:hide_window();
end

function pwg_social_control:on_unread_msg_notify(param)
    self:log("on_unread_msg_notify");
    if nil == param then
        return;
    end
    if self.UNREAD_NOTIFY_NEW == param.notify_type then
        event_center.send_event(group_event.PWG_FRIEND_HAVE_UNREAD_MESSAGE, "{}");
        self:refresh_toolbar_redpoint();
    elseif self.UNREAD_NOTIFY_REFRESH == param.notify_type then
        self:refresh_toolbar_redpoint();
    elseif self.UNREAD_NOTIFY_NEW_ROOM_MSG == param.notify_type then
        self:show_toolbar_panel_redpoint(true);
    elseif self.UNREAD_NOTIFY_NEW_GENERIC_MSG == param.notify_type then
        if not pwg_social_view:is_window_visible() then
            self:show_toolbar_panel_redpoint(true);
        end
    end
end

function pwg_social_control:on_session_state_changed(param)
    self:log("on_session_state_changed");
    if nil == param then
        return;
    end
    if self.SESSION_STATE_OPEN == param.state then
        if nil == param.user_id or "" == param.user_id or "0" == param.user_id then
            tray_manager_control.ingore_all_flash_except_preload();
        else
            local table_param = {};
            table_param["friend_id"] = param.user_id;
            event_center.send_event(group_event.FRIEND_CLEAR_MESSAGE,
                                    base_utility.table_to_json(table_param));
        end
        self:refresh_toolbar_redpoint();
    end
end

function pwg_social_control:refresh_toolbar_redpoint(args)
    local unread_msgs = pwg_social_model:get_last_message();
    if #unread_msgs == 0 then
        self:show_toolbar_panel_redpoint(false);
    else
        self:show_toolbar_panel_redpoint(true);
    end
end

function pwg_social_control:is_new_social_enabled()
    return pwg_social_model.enable_pwg_social;
end

function pwg_social_control:open_session_ui(user_id, from)
    if nil == user_id or "" == user_id then
        -- open the oldest one
        user_id = "";
        local msgs = pwg_social_model:get_last_message();
        local send_time = -1;
        for idx, msg in ipairs(msgs) do
            if -1 == send_time or msg.send_time < send_time then
                send_time = msg.send_time;
                user_id = msg.contact_id;
            end
        end
    end
    local source = "&from=client";
    if nil ~= from and "" ~= from then
        source = "&from=" .. from;
    end
    local social_url = LoadStr("SOCIAL_MAIN_PAGE_URL");
    social_url = social_url .. "?pane=session&friend_id=" .. tostring(user_id) .. source;
    comm_center.call_service("i_service_show_pwg_social_window", { show = true, url = social_url });
end

function pwg_social_control:open_room_ui(org_id, room_id, from)
    if nil == org_id then
        org_id = "";
    end
    local source = "&from=client";
    if nil ~= from and "" ~= from then
        source = "&from=" .. from;
    end
    local social_url = LoadStr("SOCIAL_MAIN_PAGE_URL");
    social_url = social_url .. "room?org_id=" .. tostring(org_id) .. source;
    if nil ~= room_id then
        social_url = social_url .. "&room_id=" .. tostring(room_id);
    end
    comm_center.call_service("i_service_show_pwg_social_window", { show = true, url = social_url });
end

function pwg_social_control:on_open_pwg_panel(data)
    if nil == data then
        comm_center.call_service("i_service_show_pwg_social_window", { show = true });
        return;
    end

    if nil ~= data.url and "" ~= data.url then
        comm_center.call_service("i_service_show_pwg_social_window", { show = true, url = data.url});
        return;
    end

    if data.session_type == self.SESSION_TYPE_1V1_SESSION then
        self:open_session_ui(data.target_id, data.from);
    elseif data.session_type == self.SESSION_TYPE_ROOM then
        self:open_room_ui(data.target_id, data.room_id, data.from);
    end
end;

function pwg_social_control:show_toolbar_panel_redpoint(to_show)
    if to_show then
        local show_param = { show = "true" };
        event_center.send_event(group_event.PWG_SHOW_SOCIAL_PANEL_RED_POINT, 
                                base_utility.table_to_json(show_param));
    else
        event_center.send_event(group_event.PWG_CLEAR_SOCIAL_PANEL_RED_POINT, {});
    end
end

function pwg_social_control:on_apply_add_friend(param)
    if pwg_social_view:is_window_visible() then
        return;
    end
    self:show_toolbar_panel_redpoint(true);
end;

function pwg_social_control:on_social_page_nav_completed()
    pwg_social_view:hide_close_button();
end;

function pwg_social_control:set_game_invitation_result(json)
    local rail_host = base_utility.get_component_adapter(
            "IRailHost", "ierd_tgp.tgp_plugin.GetRailHostAdapter();");
    if rail_host ~= nil then
        rail_host:SetGameInvitationResult(json);
    end
end;

function pwg_social_control:js_dispatch_callback(rsp_info, data)
    local param = data or {};
    if nil == rsp_info or nil == rsp_info.callback_func or "" == rsp_info.callback_func then
        return;
    end
    local rsp_tbl = web_utility.web_rsp_comm_data(rsp_info, 0, "");
    rsp_tbl.data = data;
    local rsp_json = base_utility.table_to_json(rsp_tbl);
    web_utility.lua_call_js_ex(rsp_info.callback_func, rsp_json, CID_WEB_CTRL_FOR_NEW_SOCIAL);
end;

function pwg_social_control:on_set_session_notice_status_rsp(data)
    if nil == data then return end;
    local rsp_info = pwg_social_model:pop_notice_operation(data.target_id);
    self:js_dispatch_callback(rsp_info, data);
end;

function pwg_social_control:on_query_user_muted_list_rsp(data)
    if nil == data then return end;
    self:js_dispatch_callback(pwg_social_model.muted_user_query_rsp, data);
    pwg_social_model.muted_user_query_rsp = nil;
end

function pwg_social_control:js_set_notice_status(json)
    local param = base_utility.json_to_table(json);
    if nil == param.data or type(param.data) ~= 'table' then
        self:js_dispatch_callback(param, {});
        return;
    end
    local user_id = param.data.target_user_id or "";
    if "" == user_id then
        self:js_dispatch_callback(param, {});
        return;
    end
    pwg_social_model:push_notice_operation(user_id, param);
    comm_center.call_service("i_service_new_social_set_session_notice_status", param.data);
end;

function pwg_social_control:js_query_muted_user_list(json)
    local param = base_utility.json_to_table(json);
    local ret = comm_center.call_service("i_service_new_social_query_user_muted_list", {});
    if nil == ret then
        self:js_dispatch_callback(param);
        return;
    end
    if 0 == ret.result then
        local rsp_param = {};
        if nil == ret.muted_list or type(ret.muted_list) ~= 'table' then
            rsp_param.muted_list = { };
        else
            rsp_param.muted_list = ret.muted_list;
        end
        self:js_dispatch_callback(param, rsp_param);
        return;
    end
    pwg_social_model.muted_user_query_rsp = param;
end;

pwg_social_control:init();
